﻿var page = (function () {
    function cinit(p) {
        p.ds = new datasource();
        p.ds.c = p.c;
        p.ds.init();
        initheader(p);
        initbody(p);
    }
    function serachload(p) {
        var txt = $("#search-text").textbox('getText');
        var where = [];
        if (txt != "" && txt != null) {
            for (var i in p.searchWhere) {
                where.push(p.searchWhere[i]);
                where[i].Value = txt;
            }
        }
        if (typeof p.defultWhere == 'object' && p.defultWhere.length > 0) {
            for (var i in p.defultWhere)
                where.push(p.defultWhere[i]);
        }
        else
            where.push(p.defultWhere);
        p.ds.load(where);
    }
    function initheader(p) {
        $('#page-title').html(p.title);
        $('#page-search').bind("click", function () {
            $("#search-dialog").dialog("open");
        });
        $("#search-button").bind("click", function () {
            serachload(p);
        });
    }
    function initbody(p) {
        p.ds.onLoad = function (data) {
            var rows = formatdata(p, data);
            initdatalist(p, rows);
        }
        var where = [];
        if (typeof p.defultWhere == 'object' && p.defultWhere.length > 0) {
            for (var i in p.defultWhere)
                where.push(p.defultWhere[i]);
        }
        else
            where.push(p.defultWhere);
        p.ds.load(where);
    }
    function initfooter(p) {

    }
    function formatdata(p, data) {
        var mainColumns = p.mainColumns;
        var subColumns = p.subColumns;
        var rss = [];
        var source = p.ds;
        for (var r in data.rows) {
            var rr = data.rows[r]
            var t = styletext(source, mainColumns, subColumns, rr);
            rss.push({ text: t, value: rr });
        }
        return rss;
    }
    function initdatalist(p, rows) {
        $("#page-datalist").datalist({
            fit: true,
            lines: true,
            border: false,
            textFormatter: function (value) {
                return '<a href\'javascript:void(0)\' class=\'datalist-link\'>' + value + '</a>';
            },
            onClickRow: function (index, row) {
                var f = new from();
                f.title = row.value.maintext;
                f.row = row.value;
                f.parent = p;
                f.ds = p.ds;
                f.init();
            },
            data: rows
        });
    }
    function styletext(source, maincols, subcols, row) {
        var s = "";
        var maintext = farmattext(source, maincols, row);
        var subtext = farmattext(source, subcols, row);
        var mark = "";
        if (row.REMARK && row.REMARK != "") {
            mark = row.REMARK;
        }
        if (mark != "")
            mark = "</br>" + mark;
        s += '<div style="font-size:16px;font-weight:bold">' + maintext + '</div>';
        s += '<div style="line-height:24px">' + subtext + mark + '</div>';
        row.maintext = maintext;
        return s;
    }
    function farmattext(data, cols, row) {
        var text = "";
        for (var i in cols) {
            var n = cols[i];
            var item = data.getitem(n);
            var vvv = "";
            if (item && item.foreign && item.foreign.isfkey) {
                var fn = item.foreign.displayname;
                vvv = row[fn];
            }
            if (item && item.comvtp && item.comvtp.isvtp) {
                for (var v in item.comvtp.items) {
                    var com = item.comvtp.items[v];
                    if (com.Value == row[n]) {
                        vvv = com.Text;
                        break;
                    }
                }
            }
            if (vvv == "")
                vvv = row[n];
            if (vvv != null) {
                if (text == "")
                    text = vvv;
                else
                    text += "&nbsp;" + vvv;
            }
        }
        return text;
    }
    return {
        applyTo: "page",
        ds: null,
        c: "",
        defultWhere: [],
        searchWhere: [],
        mainColumns: [],
        subColumns: [],
        title: "",
        fromconfig: {
            grid: [],
            disablebox: "",
            editbox: [],
            editindex: 0
        },
        init: function () {
            cinit(this);
            $.mobile.go("#" + this.applyTo);
        }
    }
})